package netframe;

import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;
import utils.LogUtils;
import utils.TimeUtil;


/**
 * Created by Administrator on 2016/12/15.
 */
public class GetClient {
//    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    public static OkHttpClient getClient() {
        OkHttpClient.Builder builder=new OkHttpClient.Builder();
        builder.addInterceptor(new LoggingInterceptor()).connectTimeout(30000, TimeUnit.MILLISECONDS)
                .readTimeout(30000, TimeUnit.MILLISECONDS).writeTimeout(30000,TimeUnit.MILLISECONDS);
//        if (BuildConfig.DEBUG) {
            HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
            logging.setLevel(HttpLoggingInterceptor.Level.BODY);
            builder.addInterceptor(logging);
//        }

        return builder.build();
    }

    static class LoggingInterceptor implements Interceptor {
        @Override
        public Response intercept(Interceptor.Chain chain) throws IOException {
            //发送时截取数据
            Request request = chain.request();
            long t1 = System.nanoTime();//请求发起的时间
            LogUtils.e(String.format("发送请求:%s", request.url()));

            //接收时截取数据
            Response response = chain.proceed(request);
            long t2 = System.nanoTime();//收到响应的时间
            ResponseBody responseBody = response.peekBody(1024 * 1024);
//            LogUtils.e(String.format("接收响应:%s%n请求时间:%.1fms%n报文信息:%s返回JSON:%s",
//                    response.request().url(),
//                    (t2 - t1) / 1e6d,
//                    response.headers(),
//                    responseBody.string()));

            return response;
        }
    }

    public static void saveToFile(String receiveData) {
        StringBuffer sb = new StringBuffer();
        try {

            if (Environment.getExternalStorageState().equals(
                    Environment.MEDIA_MOUNTED)) {
                DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
                String time = formatter.format(new Date());
                sb.append(time);
                sb.append("  ----------:  ");
                sb.append(receiveData);
                sb.append("  ----------;  ");
                sb.append("\r\n");
                String fileName = TimeUtil.getYear() + "-" + (TimeUtil.getMonth() + 1) + "-" + TimeUtil.getDay() + ".log";
                String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Http/";
                File dir = new File(path);
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                FileOutputStream fos = new FileOutputStream(path + fileName, true);
                fos.write(sb.toString().getBytes());
                fos.flush();
                fos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
